﻿using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using PlatformNetCore.Common.Configuration;
using PlatformNetCore.Entity;
using PlatformNetCore.Entity.Configuration;
using PlatformNetCore.IRepository;
using PlatformNetCore.Repository;
using PlatformNetCore.Test.DbHelper;
using PlatformNetCore.Test.Models;
using PlatformNetCore.Test.Options;
using System;

namespace PlatformNetCore.Test
{
    public class Common
    {
        /// <summary>
        /// 构造依赖注入容器，然后传入参数
        /// </summary>
        /// <returns></returns>
        public static IServiceProvider BuildServiceForSqlServer()
        {
            var services = new ServiceCollection();
            string connectionString = ConfigurationSetting.GetConnectionString();
            //"Data Source=localhost;Initial Catalog=PlatformNetCoreDB; user id=sa;pwd=xiaopan123456;";
            services.AddDbContext<PlatformContext>(options => options.UseSqlServer(connectionString));

            services.Configure<CodeGenerateOption>(options =>
            {
                options.ConnectionString = connectionString;
                options.DbType = DatabaseType.SqlServer.ToString();//数据库类型是SqlServer,其他数据类型参照枚举DatabaseType
                options.Author = "test";//作者名称
                options.OutputPath = "C:\\CodeGenerator";//模板代码生成的路径
                options.ModelsNamespace = "PlatformNetCore.Entity";//实体命名空间
                options.IRepositoryNamespace = "PlatformNetCore.IRepository";//仓储接口命名空间
                options.RepositoryNamespace = "PlatformNetCore.Repository";//仓储命名空间
                options.IServicesNamespace = "PlatformNetCore.IService";//服务接口命名空间
                options.ServicesNamespace = "PlatformNetCore.Service";//服务命名空间
            });
            services.Configure<DbOption>(op => {
                op.ConnectionString = connectionString;
                op.DbType = DatabaseType.SqlServer.ToString();
            });
            services.AddScoped<IMenuRepository, MenuRepository>();
            services.AddScoped<ICategoryRepository, CategoryRepository>();
            services.AddScoped<IRepository<Menu>, Repository<Menu>>();
            services.AddScoped<CodeGenerator>();
            return services.BuildServiceProvider(); //构建服务提供程序
        }
    }
}
